package cn.java71.springboot.demo1.ctrl;

import cn.java71.springboot.demo1.entity.Student;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author oldliu
 * @since 1.0
 */
@RestController
@RequestMapping("student")
public class StudentController {
    //@Value("e:/fff")
    @Value("${upload.path}")
    private String uploadPath;//路径
    @PostMapping("add")
    public String add(@ModelAttribute  Student student, @RequestPart("idcard")MultipartFile idcard){
        if(idcard!=null&&idcard.getSize()>1){
            try {
                String oldName=idcard.getOriginalFilename();
                String newName= UUID.randomUUID()+oldName.substring(oldName.lastIndexOf('.'));
                student.setIdcardpic(newName);
                idcard.transferTo(new File(uploadPath,newName));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        ///
        return "Zeng jia ok";
    }
}
